package Year21_Month10.day1028;

public class MyCircularQueue {
    int[] array;
    int front;//定义头结点
    int rear;//定义尾结点
    int count;//定义有效节点
    int N;//定义数组长度

    public MyCircularQueue(int k) {
        array = new int[k];
        N = k;
    }

    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        array[rear] = value;
        rear++;
        if(rear == N){
            rear = 0;
        }
        count++;
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        front++;
        front %= N;
        count--;
        return true;
    }

    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return array[front];
    }

    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        return array[(rear + N - 1)% N];
    }

    public boolean isEmpty() {
        return 0 == count;
    }

    public boolean isFull() {
        return count == array.length;
    }
}
